package com.key.modules.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.key.common.base.RespResult;
import com.key.common.utils.OddNumbersUtils;
import com.key.modules.dto.CarrierDTO;
import com.key.modules.entity.CarrierContract;
import com.key.modules.mapper.CarrierContractMapper;
import com.key.modules.service.CarrierContractService;
import com.key.modules.vo.CarrierVO;
import com.key.modules.vo.CarriersVO;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class CarrierContractServiceImpl implements CarrierContractService {

    @Autowired
    CarrierContractMapper contractMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public RespResult showCustomerContract(CarrierDTO carrierDTO) {
        log.info("进入查询方法，传入参数="+carrierDTO);
        Page<CarrierVO> page = new Page<>(carrierDTO.getCurrent(),carrierDTO.getSize());
        Page<CarrierVO> carrierVOPage = contractMapper.selectCarrierContractByDTO(page, carrierDTO);
        System.out.println(carrierVOPage);
        List<CarrierVO> records = carrierVOPage.getRecords();
        redisTemplate.opsForHash().put("承运商合同列表","合同",records);
        log.info("查询结果："+records);
        return RespResult.success(records);

    }

    @Override
    public RespResult addCarrierContract(CarrierContract contract) {
        log.info("进入添加方法，参数为："+contract);
        contract.setNumber(OddNumbersUtils.creatON("CYS"));
        int i = contractMapper.insert(contract);
        log.info("添加完成，添加条数为："+i);
        if (i<=0){
            return RespResult.error("添加失败");
        }
        else {
            return RespResult.success();
        }
    }

    @Override
    public RespResult delCarrierContract(Integer[] ids) {
        log.info("进入删除方法，要删除的id号："+ids);
        int i = contractMapper.deleteBatchIds(Arrays.asList(ids));
        log.info("删除的条数="+i);
        return RespResult.success();
    }

    @Override
    public RespResult updateCarrierContract(CarrierContract contract) {
        log.info("进入修改方法，要修改的订单信息："+contract);
        int i = contractMapper.updateById(contract);
        log.info("修改完成，条数="+i);
        return RespResult.success();
    }

    @Override
    public RespResult showCarrierContractById(Integer id) {
        log.info("进入id查询方法");
        CarrierContract contract = contractMapper.selectCarrierById(id);
        log.info("查询结果："+contract);
        return RespResult.success(contract);
    }
}
